fixing iff bugs, will clean the codes later