some optimizations and minor fixes