Refactor functions that modify castling permissions