Optimize code into functions.