add some comments and switch first if to look at flags