refactored shape and type inference (tested)