Refactoring and tests

Refactored the backend and added a bunch of unit tests