reformatted and rearranged code