Basically reshuffled code around for legibility