Rearrange search code