adding middleware logic (needs testing !!)