added argument to specify debug mode. also some minor refactorings