Bounds checking and fix compile error