Tidy main code