Added code to find top row of board and magic_invert