first attempt at complete rewrite