some optimizations and fixes

some optimizations and fixes