refactored: from server to middleware