Implemented last step of algorithm