Full implementation of task 4