getting closer to a working flattenbyamount impl