Adding cleanup, and cleaning up the code