Refactor to support services

Refactor to support separating the code into UI and services