cleaned up by adding a helper function: isSameType().