added a conversion to data.frame at the start, so code can also be applied to matrices