Some tweaking and refactoring.