various changes, works with arbitrary square grid