Fixed bug in middleware