Further refactors and improvements.