rewrite most of functions
finish function 5