refactoring and improve tests