Rearranged code for clarity