Added more error checking to popen