Improve getNextPipe method.