Use reopen instead of doing it by hand with fcntl
Misc cleanup