Fixes issue #998.

Fixes multiple issues with the way PR #997 was implemented. 
@TShapinsky here you're doing the same comparison several times. I pulled the result into a variable, and just referenced that.