Fix #488

Addresses #488 and improves the clarity of the logic

Please verify the change is correct.