A bit more refactoring to compact the code and reduce redundancy