Import scala snippets created by @gorodok with some alterations.