Refactoring to remove processing on the main thread.