fixed bugs; seems to work