Made the code less duplicated and easier to read