Split remove_invalid_fields() into reusable chunks