Refactor Rake task and add tests.