Moving main loop into functions to be able to call from other functions.