Refactored for better architecture