refined code more python-idiomatic