Better way to repeat a function