Made getNextEvent function more general