refactored and extended device logic. fixed bug in device logic. some code still needs to be refactored