Massive code refactor

Tidying up code so it can be passed on to someone else for future work