add some defer
move some types into types.go