Refactored code to remove some long 'if-else' blocks.