Refactored logic out of server.js and placed it into the handler along w...

...ith other server logic.