Moving code into separate functions as complexity grows