Improved lattice code readability

Thanks to @nhorelik for the vast majority of changes in this commit.