moving most of the clicking into a function