simplify logic flow, fix error message