Further refactoring and code clean-up