Refactored for clarity
Split code into separate module