lots of fixes, cleaning up and uncluttering the global namespace.