Adding Patrick's updated main and functions code