fixed some logical bugs