bug fix, seems to work, maybe needs more test