Implemetn middleware layer