Add sys_index bounds check to sys_close for robustness
Fix out-of-bounds indexing error in findnextinvalid()