Updated code for readability and modularity