updated leave function