Refactoring and improvements