logic was simplified and tests were improved