Implemented IDisposable and fixed a bug