Added some bounds checking