Moved key filtering to a separate function.