Enhanced the PR and added unit tests.