mongo fix and code refactoring