Updated code for modularity and readability