Extracted methods from main loop to keep things nicer to look at