improved bounds checking