Rewrite from all mutable to immutable