fixed various logical errors