Bug fixes and performace tweaks