fixed bugs noted by diantahoc, requires testing