Refactored main function

Split the main function into three smaller functions with individual
purposes.
Combined scripts to a single file