Working version, tested for sentences up to length 20.