fix various bugs on server side (sql) and client side