Refactored main to run function