Refactor Endpoint handler to function