fix leaky fds, add debugging for statting, clean up formatting