Restructured the code for readability