As recursive function, this could generate a lot of queries. Could be handled with single query.