Divide main function into smaller functions for better overview