Many optimizations and bug fixs.