Refactoring towards more purely functional style.