Made some of the code more compact and less repetitive.