working version with resolving plus rearranging