implemented better resource cleanup