Fix some more bugs, and use dup2 because dup leaks on some implementations.