refactored LM flattening method out into its own helper method.