some rewriting, some stuff broken for now