fixed index out of bounds issues