added command-line options and code factoring