Split to UDF

Splitting main functions to external UDFs