restructuring, debug output
added debug output