maybeRef (s:ss) n = do env <- liftIO $ readIORef s
                       case lookup n env of
                            Just v -> return $ Just v
                            Nothing -> maybeRef ss n
maybeRef (s:ss) n = do env <- liftIO $ readIORef s
                       case lookup n env of
                            Just v -> return $ Just v
                            Nothing -> maybeRef ss n
maybeRef (s:ss) n = do env <- liftIO $ readIORef s
                       case lookup n env of
                            Just v -> return $ Just v
                            Nothing -> maybeRef ss n