I haven't tried to vectorise this yet, to keep it easier to read...