Added command line options, many bug fixes