Refactor towards a stream-oriented implementation