Further refactoring and improvements