fixed a few logic errors