Replaced more duplicate code with helper functions.