organized version with debug loging