Full implementation of task 2