Repeated code was replaced.
Added few tests.