fixed bug with range [1, 1], some refactoring too