Fixed bugs on range checking

- Now all unit tests pass OK