Refactoring for readibility and idiomatic python