Added middleware and more code