some refactoring in preparation for adding forward declarations.
added support for forward declarations. you monsters.