Various typo fixes and cleaning. Added mode 0 for running from external script