moves logic out of stream, prevents memory leak