Implementation of flatten for nnhp