fixing the index out of bound error finally