Added proper bounds checking.