Copy flatten method from helpers.coffee