CFY-1000 lots of code cleanup and some optimizations