Change drainbuffer to iteration from recursion