Did some more work on the main function