tidy up and debug