Finish homework3 part3. Works but need code improvement