minor fixes and some optimizations