Add boundary check
Merge Kenny's commit for boundary check
Merge branch 'Kentaxer-master'