diff --git a/include/nif_math.h b/include/nif_math.h
index 2c389be35c3a07bb4ea1e99e331d063afe5159f9..b329c7e62e6a37881a2ec1d9ff0f941c49bbbfb7 100644
--- a/include/nif_math.h
+++ b/include/nif_math.h
@@ -16,6 +16,10 @@ namespace Niflib {
 #define PI 3.14159265358979323846f  //Probably more accurate than a float can be, but it will just be rounded off anyway
 #endif
 
+#ifndef _countof
+#define _countof(x) ((sizeof (x))/(sizeof((x)[0])))
+#endif
+
 //Forward declarations
 struct TexCoord;
 struct Triangle;
@@ -1065,7 +1069,7 @@ struct InertiaMatrix {
 	/*! Copy constructor.  Initializes Matrix to another InertiaMatrix.
 	* \param[in] m The matrix to initialize this one to. 
 	*/
-	NIFLIB_API InertiaMatrix( const InertiaMatrix & m ) { memcpy(rows, m.rows, sizeof(Float4) * 4); }
+	NIFLIB_API InertiaMatrix( const InertiaMatrix & m ) { memcpy(rows, m.rows, sizeof(Float4) * _countof(rows)); }
 
 	/*! This constructor can be used to set all values in this matrix during initialization
 	* \param[in] m11 The value to set at row 1, column 1.
diff --git a/src/nif_math.cpp b/src/nif_math.cpp
index d368a2f24e54711c1ef94b35a1e921e99bdac0d1..48f317b70effe247ec9d830acf80330422093f50 100644
--- a/src/nif_math.cpp
+++ b/src/nif_math.cpp
@@ -684,7 +684,7 @@ InertiaMatrix & InertiaMatrix::operator*=( const InertiaMatrix & rh ) {
 	InertiaMatrix r;
 	InertiaMatrix & lh = *this;
 	float t;
-	for (int i = 0; i < 3; i++) {
+	for (int i = 0; i < _countof(rows); i++) {
 		for (int j = 0; j < 4; j++) {
 			t = 0.0f;
 			for (int k = 0; k < 4; k++) {
@@ -703,7 +703,7 @@ InertiaMatrix InertiaMatrix::operator*( float rh ) const {
 }
 
 InertiaMatrix & InertiaMatrix::operator*=( float rh ) {
-	for (int i = 0; i < 3; i++) {
+	for (int i = 0; i < _countof(rows); i++) {
 		for (int j = 0; j < 4; j++) {
 			(*this)[i][j] *= rh;
 		}
@@ -726,7 +726,7 @@ InertiaMatrix InertiaMatrix::operator+( const InertiaMatrix & rh ) const {
 } 
 
 InertiaMatrix & InertiaMatrix::operator+=( const InertiaMatrix & rh ) {
-	for (int i = 0; i < 3; i++) {
+	for (int i = 0; i < _countof(rows); i++) {
 		for (int j = 0; j < 4; j++) {
 			(*this)[i][j] += rh[i][j];
 		}
@@ -735,12 +735,12 @@ InertiaMatrix & InertiaMatrix::operator+=( const InertiaMatrix & rh ) {
 }
 
 InertiaMatrix & InertiaMatrix::operator=( const InertiaMatrix & rh ) {
-	memcpy(rows, rh.rows, sizeof(Float4) * 4);
+	memcpy(rows, rh.rows, sizeof(Float4) * _countof(rows));
 	return *this;
 }
 
 bool InertiaMatrix::operator==( const InertiaMatrix & rh ) const {
-	for (int i = 0; i < 3; i++) {
+	for (int i = 0; i < _countof(rows); i++) {
 		for (int j = 0; j < 4; j++) {
 			if ( (*this)[i][j] != rh[i][j] )
 				return false;
@@ -750,7 +750,7 @@ bool InertiaMatrix::operator==( const InertiaMatrix & rh ) const {
 }
 
 bool InertiaMatrix::operator!=( const InertiaMatrix & rh ) const {
-	for (int i = 0; i < 3; i++) {
+	for (int i = 0; i < _countof(rows); i++) {
 		for (int j = 0; j < 4; j++) {
 			if ( (*this)[i][j] != rh[i][j] )
 				return true;
@@ -769,7 +769,7 @@ InertiaMatrix InertiaMatrix::Transpose() const {
 Matrix33 InertiaMatrix::Submatrix( int skip_r, int skip_c ) const {
 	Matrix33 sub;
 	int i = 0, j = 0;
-	for (int r = 0; r < 3; r++) {
+	for (int r = 0; r < _countof(rows); r++) {
 		if (r == skip_r)
 			continue;
 		for (int c = 0; c < 4; c++) {
@@ -793,7 +793,7 @@ InertiaMatrix InertiaMatrix::Inverse() const {
 	InertiaMatrix result;
 
 	float det = Determinant();
-	for (int r = 0; r < 3; r++) {
+	for (int r = 0; r < _countof(rows); r++) {
 		for (int c = 0; c < 4; c++) {
 			result[c][r] = Adjoint(r, c) / det;
 		}