refactoring the code
adding regression tests