a more robust implementation of capture_stdout - now accepting input