tidying up code, lots of testing