remove duplicate code for 3D and 4D input