Refactored for ease of debugging