fix potential out-of-bounds error in client/merklemap; refactor