Adding checks to prevent null pointers on bad data