Mongoid support improvements and refactoring