Refactoring and added new tests.