added out of bounds checking