added bounds checking