Fixed several bugs that prevented the reference_piece step of castling from working