factorise code and add click to marker