Code cleanup and variable ordering