fixed bugs, now seems to work