Refactored to use case classes and immutability.