fix a few logical errors