Made some improvements to the code

Removed some repetition in the code