More resource leaks and fixes