Many logic fixes