This took an eternity. Don't code when tired.