Made InstanceMapping a class rather than namedtuple impl. Refactored code and added docstrings