Refactor to allow re-use with $location service

Refactor to allow re-use with $location service when used inside controller code.