Add unitTests and Refactoring