Added the initialization of several locals. In check_fields, validate that we have a valid index (idx) before using.