move if to when, and misc cleanups