Fixed logical bugs