remove code duplication and add few tests
Please enter the commit message for your changes. Lines starting