added some bounds checking