Restructured code with exceptions
code updated to handle device add edge cases