Cleanup for better exception handling

Move listeners to separate functions to avoid excessive nests on try
catch blocks, plus sending back to the device a "helful" error message