Rearranged and split code to improve readability.