Well.... umm

I'll try to understand the logic....and then re-factor it

Plz help me.. I'm getting a feeling that its only with a few lines of codes but i'm not able to work it out