fixed problems with ranges out of bounds