Refactor sanity middleware into separate branch and remote middleware