Edited to fix bugs as for HW6