simplify some logic, improve tests