added --mode and -n options, bug fixes