Fixing some logic problems