Fixes problems with middleware in mongoose