fix index out of bound. wrong order