updated code - untested