Proper way of freeing resources