Refactored into more generic methods and variables.