updated code, untested (attempt: 1)