Converting scripts into functions