Added error handling and fixed stdin/out for pipes.