Factored out code into a class
Tidy up