reorderd response building logic (to fix error handling bug)