improved script and added debug info