refactor device code
complete refactor