Closes #190 - Fix all potential resource leaks

Pull request for #190
hehe, hopefully we can learn from these types of API mistakes and try to avoid making similar ones.