Moved around the res.end() calls and added documentation to code.