extract code into function