Bug fixes

bug fixes and performance improvements