Refactor code to improve modularity and readability