fix bugs and cleaned functions