Just a quick example - could be improved more and made fully recursive.