Condensed the code in some places.