complete rewrite of code