small improvement, use when instead of if