Issue #116 add range check and simplify some conditions