Fix messy code problems. Complete all function.