Fixing some logic bugs