Massive scala rewrite. Part 2