reworked router.js to avoid reduntant repition in the code