refactor popen