almost complete rewrite...