Making code more pythonic