sync function modified to take care of categories