updated function now works