bug fix

a rough bug fix, see code