added debug messages and some comments