A more pythonic implementation