added some bug checking