Added command line options and many bug fixes