Fix errors thrown and greatly speed up processing of invalid files