fixed 'index out of range' error