Finished writing part 3. All parts except 4 are done, but need to be verified with homemade test cases.