Changed scope of a ton of stuff to provide a clean interface to outside.