key fixes and minor optimizations