Fixed some bounds checking issues.