Refactors the huge copypastas with nicer loops