One set of parenthesis was missing, causing a legitimate compiler
warnings. In the end it doesn't actually matter, because it just
ends up doing an unnecessary check in the w > 0 case.
This fixes the logic and moves it out into a separate functions,
to be a bit more readable.