move code  to functions to arrange it a little