Finally got a function working. Procedure.