optimizations and small fix