Some code cleanup, added debug output