Some error handling cleanups in search() and by_key()