Initial import of working middleware code