refactored implementation of wrap