Made code more efficient.

Thanks to the feedback from @jashsayani