Fix some potential resource leaks