Refactor and bugfix: now ranges work