fixed some nasty bugs