Fix bugs on resource management