Refactoring: split up code into smaller functions.