Some refactoring, but more is needed.