Refactored ugly 'case' statement