refactor, reduce nesting

not sure if my python is 100% correct, its syntactically correct according to the interpreter, but had an problem withs some dependencies