added some assertions and logging