Refactor backend calls into a factory.
Refactor and clean up some code