Applying patch from Ethan to fix up style and simplify a little bit.