extended Kai's work, almost finished assignment 4.