extracts code into function