refactor code, use functions to make code more readable