handled corner cases, tested on toy inputs, but not on real data yet