Implementation of HW4 functions