Clean internals

a fairly large (but backwards compatible) rearranging of the inner parts.