Made the code more pythonic