Moved code around for legibility