@bronevet I have rewritten the function based on your suggestion. Please take a look