Debugged and tidy up