Minor tweaks to code, and fix for order of operations.