fixed some subtle bugs