factored out a bunch of stuff. not sure what to do with the Entity base class though.