Did some tidying of code and refactoring.