factor out complexity and make script work better