Refactored for learning purpose