Condensed code and made it neater