Thanks a million for pointing out the offending line.
As you correctly said i was unnecessarily doing those assignments (when there was no need of them).
Just by implementing your first suggestion (removing the for @validbufs part) helped bring the runtime to say 2 minutes (from the huge runtime of more than 2 hours (probably even more) that my erroneous code was doing earlier)
Thanx again