added debug mode, better directory handling, etc