moved middleware-like functions out of api section and into middleware section