fix middleware insertion