updated key filtering code from Keith's branch