This must have been fun to write / debug :)