Working version but not particularly fast