Cleaned code.

Put repeating code into a private method.