Refactor and logic tweak