Reorganized code for readability