Fix numerous shape checking errors

Initial error pointed to by David Neumann, so went on a hunt. Still no
guarantees.