Refactored and simplified code in order to improve readability and testability.