Added support for multiple devices, needs testing.