Fixed bug in resource management