Maybe start with something like this:
; ===========================================================================================
;
;     Script Information
;    
;     Title:      
;     Author:      
;     Description:
;    
;
; ===========================================================================================
;Script Options

If Not @LOGONMODE
    Break On
Else
    Break Off
EndIf
Dim $RC
$RC = SetOption("Explicit", "On")
$RC = SetOption("NoMacrosInStrings", "On")
$RC = SetOption("NoVarsInStrings", "On")
If @SCRIPTEXE = "KIX32.EXE"
    $RC = SetOption("WrapAtEOL", "On")
EndIf

;Declare variables

;Initialize variables

;Code

;Personal UDF Section

;UDF Section

Now paste the UDF you found at the bottom
Declare your variables under
;Declare variables
Write your code under
;Code
Maybe you can find your own way to structure your code afterwards