Changed code to use functions