Implement more robust `keys` function