Working implementation of homework 5