Working solution as described in class