I have duplicated code in order to avoid performing the same test twice (line 89).