Optimize Queries
Added check for empty databases