Closes #190 - Fix all potential resource leaks

Pull request for #190