Fix null reference exception on Exit