private static void processNextKey() {
        WatchKey nextKey;
        try {            
            nextKey = watchService.take();  // wait for the next key to throw an event
            
            // iterate the events that occured on this key
            for (WatchEvent watchEvent : nextKey.pollEvents()) {
                WatchEvent<Path> ev = (WatchEvent<Path>) watchEvent;
                
                // get the file responsible for this event
                Path fullPath       = watchKeyToPath.get(nextKey).resolve(ev.context());
                File responsibleFile = fullPath.toFile();
                
                // if a directory was deleted, remove references to the directory
                if (!responsibleFile.exists()){
                    if (registeredDirectories.contains(responsibleFile))
                        unregisterDirectory(responsibleFile);
                } 
                
                // if a new directory was created, watch the directory
                else if (responsibleFile.isDirectory()) {
                    if (ev.kind().equals(StandardWatchEventKinds.ENTRY_CREATE))
                        registerDirectory(responsibleFile);
                } 
                
                // if file was created or modified, scan the file
                else if (responsibleFile.isFile()) {
                    ScanManager.pushFileForUpload(responsibleFile, false);
                }
            }
            
            nextKey.reset();    // reset the key in order to process the next one
        } catch (InterruptedException | NullPointerException e) {}
    }