added prefix function to find only longest prefix of key