Eliminate extraneous loop in pipe server and refactor code.