Split code into functions to make it easier to maintain