Resource manager code, using recursive thinking