modified from CapDEM