better client cleanup
!! NEEDS TO BE TESTED !!
TODO: add locks and more thread safety