starting to tidy up and rewrite