reorganized and formatted code