Revised code to use functions