Refactored to work in a multi-threaded env such as Sidekiq.