Add leave function and a bit cleanup