optimizations for performance and fixes