Tidied, refactored, updated tests