rearranged coding to improve readability