fixed bug when closing device, some cleanup