Refactoring to use while on total rather than if on input.