refactored original scripts into 4 separate parts