Add option parsing and fix threading