Refactor to create a library separated from rake tasks