Made the code a little more resiliant to missing data