Cleaning up the callback code to allow for testing.