Simplifying complex algorithms with immutable data structures