Fix null reference exceptions