updated the code by separating function definitions from the rest of the code