unlock_ref(): move definition higher in the file