refactored old MADM lab4