Refactored handler loop to facilitate calling locally