Move prepareLatch to local scope