If I understood the question and if I understand what this function is supposed to do then I think this link answers your question:
Andrew ๐