Refactor controller.py and minor corrections in mongodb part