Rewrite sync class; fix bug in batch db query