cleanup some codes for implementating the middleware feature