add cycle detection, spec tests