bug fix: check index
init implement
bug fix: corner case n=0
minor change