refactor range calculation