Refactor direction code for easy reversals
Merge branch 'headmerge' into lattice_rewrite