finished refactoring. remaining messiness is mostly special-case castling stuff.