Fix logic of conditional and make it more pythonic