added command line options and fixed thread join issue