fixed few logic errors