Refactored and simplified