fix overflow and more minor cleanup