cleanup logic, and some modernizations