Prova a vedere anche questa implementazione suggerita su StackOverflow.

Ciao!