add function and fix bugs