Removed impossible branches
Refactored code to improve the testability