improved implementation of 'flatten' and fixed some bugs
updated docs