implement middleware