Refactored to working code.