rewrote the code structure; added command line options
minor changes