add Tests and refactoring