major refactoring to make server.js lean