Implement a bunch of IDisposable interfaces and clean up the code a bit.