New functions and bugs fixed