solution to flattening.js