completed the wrapping algorithm