some minor problems with previous function are now resolved