Index: branches/terrain/src/lib/graphics/importer/terrain/buffer_broker.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/buffer_broker.h	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/buffer_broker.h	(revision 8775)
@@ -19,5 +19,5 @@
 #include "glincl.h"
 #include "debug.h"
-
+#include "types.h"
 /**
  * @brief 	Manages the streaming of the vertex buffer objects for the terrain page.
@@ -55,4 +55,17 @@
 			}
 			head = tail = NULL;
+		}
+		~BufferBroker()
+		{
+			pBufferPair bp = head;
+			while ( bp ) {
+				pBufferPair tmp = bp->next;
+				release( bp->vertexName, bp->indexName );
+				bp = tmp;
+			}
+			glDeleteBuffersARB( capacity, freeVertexBuffers );
+			glDeleteBuffersARB( capacity, freeIndexBuffers );
+			SAVE_DELETE_ARRAY( freeIndexBuffers );
+			SAVE_DELETE_ARRAY( freeVertexBuffers );			
 		}
 		inline int getIBSize() { return ibSize; }
Index: branches/terrain/src/lib/graphics/importer/terrain/frustum.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/frustum.h	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/frustum.h	(revision 8775)
@@ -18,4 +18,7 @@
 #include "glincl.h"
 #include <stdio.h>
+
+
+#define MAX_CLIP_DISTANCE 400.0f
 #define m( _row,_col )  _m[_col*4+_row-5]
 
@@ -95,4 +98,5 @@
 							 m( 3, 3 ) + m( 4, 3 ),
 							 m( 3, 4 ) + m( 4, 4 ) );
+							
 			planes[FAR].setCoefficients( 
 							-m( 3, 1 ) + m( 4, 1 ),
@@ -100,4 +104,5 @@
 							-m( 3, 3 ) + m( 4, 3 ),
 							-m( 3, 4 ) + m( 4, 4 ) );
+							
 			planes[BOTTOM].setCoefficients(
 							 m( 2, 1 ) + m( 4, 1 ),
@@ -105,4 +110,5 @@
 							 m( 2, 3 ) + m( 4, 3 ),
 							 m( 2, 4 ) + m( 4, 4 ) );
+							
 			planes[TOP].setCoefficients(  
 							-m( 2, 1 ) + m( 4, 1 ),
@@ -110,4 +116,5 @@
 							-m( 2, 3 ) + m( 4, 3 ),
 							-m( 2, 4 ) + m( 4, 4 ) );
+							
 			planes[LEFT].setCoefficients(  
 							 m( 1, 1 ) + m( 4, 1 ),
@@ -115,4 +122,5 @@
 							 m( 1, 3 ) + m( 4, 3 ),
 							 m( 1, 4 ) + m( 4, 4 ) );
+							
 			planes[RIGHT].setCoefficients(
 							-m( 1, 1 ) + m( 4, 1 ),
@@ -120,4 +128,8 @@
 							-m( 1, 3 ) + m( 4, 3 ),
 							-m( 1, 4 ) + m( 4, 4 ) );
+							
+			if ( planes[NEAR].d +planes[FAR].d  > MAX_CLIP_DISTANCE ) {
+				planes[FAR].d = -planes[NEAR].d+MAX_CLIP_DISTANCE;
+			}
 		}
 		
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain.cc	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain.cc	(revision 8775)
@@ -176,5 +176,6 @@
 	pTerrainPage page = NULL;
 	frustum->extractPlanes();
-	
+	glPushAttrib( GL_ALL_ATTRIB_BITS );
+	glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
 	/*
 	 * Enable texture and vertex arrays for the first and the second texture
@@ -256,5 +257,4 @@
 			indices, GL_DYNAMIC_DRAW_ARB );
 	}
-	glPushAttrib( GL_COLOR_BUFFER_BIT );
 	glEnable( GL_BLEND );
 	glDepthFunc( GL_LEQUAL );
@@ -323,16 +323,10 @@
 	glActiveTextureARB( GL_TEXTURE1_ARB );
 	glDisable( GL_TEXTURE_2D );
-	glDisableClientState( GL_VERTEX_ARRAY );
-	glDisableClientState( GL_TEXTURE_COORD_ARRAY );	
-	glDisableClientState( GL_VERTEX_ARRAY );
-	glDisableClientState( GL_TEXTURE_COORD_ARRAY );	
 	glActiveTextureARB( GL_TEXTURE0_ARB );
-	glClientActiveTextureARB( GL_TEXTURE0_ARB );	
-	glDisableClientState( GL_VERTEX_ARRAY );
-	glDisableClientState( GL_TEXTURE_COORD_ARRAY );	
 	glEnable( GL_LIGHTING );
 	glMatrixMode( GL_TEXTURE );
 	glLoadIdentity();
 	glPopAttrib();
+	glPopClientAttrib();
 }
 
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain.h	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain.h	(revision 8775)
@@ -97,4 +97,16 @@
 		}
 		
+		~Terrain( ) 
+		{
+			//The pages are deleted through the SAVE_DELETE( root ) call.
+			SAVE_DELETE_ARRAY( pages );
+			SAVE_DELETE_ARRAY( vertices );
+			SAVE_DELETE_ARRAY( indices );
+			SAVE_DELETE( lightmap );
+			SAVE_DELETE( root );
+#ifdef USE_VBO
+			SAVE_DELETE( broker );
+#endif			
+		}
 		/**
 		 * Draws the terrain.
@@ -119,6 +131,4 @@
 		 * coordinates.
 		 */
-		inline void incActivated() { activatedCount++; }
-		inline void incDeactivated() { deactivatedCount++; }
 		inline float getAltitude( int _x, int _z ) const;
 		
@@ -150,7 +160,5 @@
 		
 		inline void setScale( const Triple& _scale )
-		{
-			scale = _scale;
-		}
+		{ scale = _scale; }
 #ifdef USE_VBO
 		inline pBufferBroker getBufferBroker() { return broker; }
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8775)
@@ -26,4 +26,12 @@
 	}\
 	while ( 0 )
+	
+TerrainPage::~TerrainPage( )
+{ 
+	SAVE_DELETE_ARRAY( indices );
+	SAVE_DELETE_ARRAY( vertices );
+	SAVE_DELETE_ARRAY( indexHash );		
+	SAVE_DELETE_ARRAY( errors );	
+}
 
 TerrainPage::TerrainPage( Terrain *_owner, int _xOffset, int _zOffset )
@@ -668,13 +676,8 @@
 	owner->getBufferBroker()->release( vbIdentifier, ibIdentifier );	
 #endif
-	if ( vertices ) {
-		delete[] vertices;
-		vertices = NULL;
-	}
-	if ( indices ) {
-		delete[] indices;
-		indices = NULL;
-		numIndices = 0;
-	}
+	SAVE_DELETE_ARRAY( vertices );
+	numVertices = 0;
+	SAVE_DELETE_ARRAY( indices );
+	numIndices = 0;
 	deactivate();
 }
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h	(revision 8775)
@@ -43,4 +43,5 @@
 		enum { TP_LEFT = 0, TP_RIGHT = 1, TP_BOTTOM = 2, TP_TOP = 3 };
 		const static int MAX_LODS 			= 5;
+		
 		/**
 		 * Creates a new terrain page with its lower left corner set
@@ -58,11 +59,5 @@
 		inline bool isActive() { return active; }
 		
-		
-		~TerrainPage( )
-		{ 
-			if ( isVisible )
-				hide(); 
-		}
-		
+		virtual ~TerrainPage();
 		/**
 		 * @brief Makes the terrain look as if it were created with the given level of 
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_quad.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_quad.cc	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_quad.cc	(revision 8775)
@@ -21,4 +21,5 @@
 {
 	scale = owner->getScale();
+	children[0] = children[1] = children[2] = children[3] = NULL;
 }
 
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_quad.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_quad.h	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_quad.h	(revision 8775)
@@ -32,7 +32,8 @@
 		virtual ~TerrainQuad( ) 
 		{
-			for ( int i = 0; i < 4; ++i )
-				if ( children[i] != NULL )
-					delete children[i];					
+			for ( int i = 0; i < 4; ++i ) {
+				if ( !children[i]->isChildless() )
+					SAVE_DELETE( children[i] );
+			}	
 		}
 		int cull( );
Index: branches/terrain/src/lib/graphics/importer/terrain/types.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/types.h	(revision 8770)
+++ branches/terrain/src/lib/graphics/importer/terrain/types.h	(revision 8775)
@@ -21,4 +21,7 @@
 #include <math.h>
 #include "texture.h"
+
+#define SAVE_DELETE_ARRAY( _a ) if ( _a ) { delete[] _a; _a = NULL; }
+#define SAVE_DELETE( _a ) if ( _a ) { delete _a; _a = NULL; }
 
 struct Triple {
Index: branches/terrain/src/lib/sound/ogg_player.cc
===================================================================
--- branches/terrain/src/lib/sound/ogg_player.cc	(revision 8770)
+++ branches/terrain/src/lib/sound/ogg_player.cc	(revision 8775)
@@ -399,10 +399,10 @@
     if(size == 0)
       return false;
-/*#ifdef SDL_BIG_ENDIAN
-			int cnt = wavLength/2;
-			Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer;
-			for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
-				*wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
-#endif*/
+#ifdef SDL_BIG_ENDIAN
+	int cnt = OGG_PLAYER_BUFFER_SIZE/2;
+	Uint16* wavBufferAsShorts = ( Uint16* )pcm;
+	for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
+		*wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
+#endif
     alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
     if (DEBUG_LEVEL >= 3)
Index: branches/terrain/src/lib/sound/sound_buffer.cc
===================================================================
--- branches/terrain/src/lib/sound/sound_buffer.cc	(revision 8770)
+++ branches/terrain/src/lib/sound/sound_buffer.cc	(revision 8775)
@@ -76,10 +76,10 @@
     }
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
-		if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) ) {
-			int cnt = wavLength/2;
-			Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer;
-			for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
-				*wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
-		}
+	if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) ) {
+		int cnt = wavLength/2;
+		Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer;
+		for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
+			*wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
+	}
 #endif
     alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec),
Index: branches/terrain/src/world_entities/camera.cc
===================================================================
--- branches/terrain/src/world_entities/camera.cc	(revision 8770)
+++ branches/terrain/src/world_entities/camera.cc	(revision 8775)
@@ -83,5 +83,5 @@
 {
   this->nearClip = nearClip;
-  this->farClip = 400.0f;//farClip;
+  this->farClip = farClip;
 }
 
