added `flatten` function plus a bit of PEP8