Split some code into separate functions.