add InRange and some refactoring