Optimizations and fixes to lattice code.