Cleans debugging variables and tidy up the code