fixing some nasty bugs