rebased for #177, add test