Don't leak fd
Tweaks and optimizations