Added getopt parameter parsing and some error checks