re-organize cast and add casts. Fix #166