refactoring registry and dereferencer

@fosdev @chaimsolomon Please have another look. This is major refactoring and I hope I made things easier to understand and more readable. Some of the registry_specs (6) are failing, which I would address tomorrow.