bug and logic fixes