replace with a simplified implementation to alleviate memory leak