Switched to GetOpts and fixed some bugs