Fixed some bugs accessing the device and cleaning up code