Refactored tests for new swituation
Refactored code for generic error handeling
Refactored duplicatei code