Reverting previous commit, fixing more resource leaks