moved some middleware stuff out to its own file.