Added Search API.

Also refactored some code related to the db.