added some more checks to avoid breaking because of unexpected data