US stocks close with slight gains after worst week of 2023