I had tried this code.
This will provide same result according to this senario.
Can you provide a working function so that I will find out possible bugs.