flatten method (needs testing) and keys method