Would it be possible to write this in an immutable way?