some bugfix and better middleware