fix some logic, and cleanup