rearranged and cleaned up