Updated middleware for error checking.