Refactor the code, no functional changes.

Use cond instead of unless + if to reduce indentation, extract a common
subexpression to a variable, introduce a variable so a multiline function
call can be written in a single line.