Refactored to be closer to the real problem