Refactored code.

Preparation for example 5.