rewrite of main code for setup

rewrite of code just started