Refactored middleware and npm