New functions and code cleanup