A lot of code tidying up/refactoring.