More optimizations and bug fixes.