Minor fixes suggested by @pekrau
Refactoring code to return dicts instead of tuples. General clean up.
Testing and fixing inevitable bugs from changes..