Improve the cleanup function, PEP8 fixes, documentation tweaks.