Fixed numerous memory leaks and added new option for specifying icon

I fixed a number of memory leaks and added a new option for specifying an icon. 

I standardised the options parsing in general around getopt so now they key option is specified with a -k <key> on the command line.