fixed various basic logic errors