Change code segments into functions