Refactored to use immutables where appropriate for increased performance.