Cleanup and some performance improvement