refactoring and improved tests