This code was made thanks to this Stack Overflow answer made by the user markE.