merged some changes from multiple_outs branch.