Split code up into more atomic functions