Structured code into function, rerendering somewhat done

Rerendering has to be improved but later ab ghissna hai