Some refactoring to cleanup code.