final fixes and optimizations