Final bit of refactoring to modularize the logic in adapter.js