cleand up code and add run function