tidy up code and rewrite services