Added the code to handle freeing frames if no frames are available.