complete rewrite of the code