major rewrite/refactor. lots of stuff thrown away here.