Code cleanup for caching and optimizer