changed back to damicos version of function