Ironed out the chinks in using mongodb and refactored code for js