moved the logic to separate functions in main