Refactor to function.
Tests added.