Refactored to much simpler design.