Restructuring program to split it up into more functions for easier maintainability