Tidy up main code