Refactor of search and some database connection stuff