Fixed js error when search query isn't present.
Cleaned up code.
Added logic to handle couchdb conflicts.