Further refactoring and cleanups.