Some refactoring for rebase behavior, tests added
moved docstring