refactored code streamlined and reogranized