Optimize pipe input read and some minor cleanup and fixes.