diff --git a/src/heightmap.cpp b/src/heightmap.cpp
index 5acb8916b0ce64fc776a123db42397ec7d601ec6..6e785fcc5868e42a4ce028b2faf12c1b0d01aa7f 100644
--- a/src/heightmap.cpp
+++ b/src/heightmap.cpp
@@ -143,7 +143,7 @@ void FixedHeightmap::makeDiamond(
 
 	f32 n = avgDiagNeighbours(center, a/2);
 	// Add (-1.0...1.0) * randmax
-	n += ((float)rand() / (float)(RAND_MAX/2) - 1.0)*randmax;
+	n += ((float)myrand() / (float)(MYRAND_MAX/2) - 1.0)*randmax;
 	bool worked = setGroundHeightParent(center, n);
 	
 	if(a >= 2 && worked)
@@ -176,7 +176,7 @@ void FixedHeightmap::makeSquare(
 
 	f32 n = avgNeighbours(center, a/2);
 	// Add (-1.0...1.0) * randmax
-	n += ((float)rand() / (float)(RAND_MAX/2) - 1.0)*randmax;
+	n += ((float)myrand() / (float)(MYRAND_MAX/2) - 1.0)*randmax;
 	bool worked = setGroundHeightParent(center, n);
 	
 	if(a >= 4 && worked)
diff --git a/src/main.cpp b/src/main.cpp
index 2f22caeb011b499cc5fe076eba5d6048cd426154..bd0ebd8cae79f0e78a16423483e6fd12e466d75c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -820,7 +820,7 @@ class RandomInputHandler : public InputHandler
 
 	s32 Rand(s32 min, s32 max)
 	{
-		return (rand()%(max-min+1))+min;
+		return (myrand()%(max-min+1))+min;
 	}
 private:
 	bool keydown[KEY_KEY_CODES_COUNT];
@@ -830,124 +830,6 @@ class RandomInputHandler : public InputHandler
 	bool rightclicked;
 };
 
-#if 0
-void updateViewingRange(f32 frametime, Client *client)
-{
-	// Range_all messes up frametime_avg
-	if(draw_control.range_all == true)
-		return;
-
-	float wanted_fps = g_settings.getFloat("wanted_fps");
-	
-	// Initialize to the target value
-	static float frametime_avg = 1.0/wanted_fps;
-	//frametime_avg = frametime_avg * 0.9 + frametime * 0.1;
-	//frametime_avg = frametime_avg * 0.7 + frametime * 0.3;
-	//frametime_avg = frametime_avg * 0.5 + frametime * 0.5;
-	//frametime_avg = frametime_avg * 0.0 + frametime * 1.0;
-	frametime_avg = frametime_avg * 0.7 + frametime * 0.3;
-
-	static f32 counter = 0;
-	if(counter > 0){
-		counter -= frametime;
-		return;
-	}
-	//counter = 1.0; //seconds
-	counter = 0.5; //seconds
-	//counter += 0.1; //seconds
-	//counter = 0.3; //seconds
-
-	//float freetime_ratio = 0.2;
-	//float freetime_ratio = 0.4;
-	float freetime_ratio = FREETIME_RATIO;
-
-	float frametime_wanted = (1.0/(wanted_fps/(1.0-freetime_ratio)));
-
-	//float fraction = sqrt(frametime_avg / frametime_wanted);
-	//float fraction = pow(frametime_avg / frametime_wanted, 1./3);
-
-	float fraction_unbiased = frametime_avg / frametime_wanted;
-
-	float fraction = pow(fraction_unbiased, 20./(float)draw_control.wanted_range);
-	
-	/*float fraction = 1.0;
-	// If frametime is too high
-	if(fraction_unbiased > 1.0)
-		fraction = pow(fraction_unbiased, 1./2);
-	// If frametime is too low
-	else
-		fraction = pow(fraction_unbiased, 1./5);*/
-
-	/*float fraction = sqrt(frametime_avg / frametime_wanted) / 2.0
-			+ frametime_avg / frametime_wanted / 2.0;*/
-	
-	//float fraction = frametime_avg / frametime_wanted;
-
-	static bool fraction_is_good = false;
-	
-	//float fraction_good_threshold = 0.1;
-	//float fraction_bad_threshold = 0.25;
-
-	/*float fraction_good_threshold = 0.075;
-	float fraction_bad_threshold = 0.125;*/
-	// If frametime is too low
-	/*if(fraction < 1.0)
-	{
-		fraction_good_threshold = pow(fraction_good_threshold, 4);
-		fraction_bad_threshold = pow(fraction_bad_threshold, 4);
-	}*/
-
-	float fraction_good_threshold = 0.23;
-	float fraction_bad_threshold = 0.33;
-	
-	float fraction_limit;
-	// Use high limit if fraction is good AND the fraction would
-	// lower the range. We want to keep the range fairly high.
-	if(fraction_is_good && fraction > 1.0)
-		fraction_limit = fraction_bad_threshold;
-	else
-		fraction_limit = fraction_good_threshold;
-
-	//if(fabs(fraction - 1.0) < fraction_limit)
-	if(fabs(fraction_unbiased - 1.0) < fraction_limit)
-	{
-		fraction_is_good = true;
-		return;
-	}
-	else
-	{
-		fraction_is_good = false;
-	}
-
-	//dstream<<"frametime_avg="<<frametime_avg<<std::endl;
-	//dstream<<"frametime_wanted="<<frametime_wanted<<std::endl;
-	/*dstream<<"fetching="<<client->isFetchingBlocks()
-			<<" faction = "<<fraction<<std::endl;*/
-
-	JMutexAutoLock lock(g_range_mutex);
-	
-	s16 viewing_range_nodes_min = g_settings.getS16("viewing_range_nodes_min");
-	s16 viewing_range_nodes_max = g_settings.getS16("viewing_range_nodes_max");
-
-	s16 n = (float)draw_control.wanted_range / fraction;
-	if(n < viewing_range_nodes_min)
-		n = viewing_range_nodes_min;
-	if(n > viewing_range_nodes_max)
-		n = viewing_range_nodes_max;
-
-	bool can_change = true;
-
-	if(client->isFetchingBlocks() == true && n > draw_control.wanted_range)
-		can_change = false;
-	
-	if(can_change)
-		draw_control.wanted_range = n;
-
-	/*dstream<<"draw_control.wanted_range = "
-			<<draw_control.wanted_range<<std::endl;*/
-}
-#endif
-
 void updateViewingRange(f32 frametime_in, Client *client)
 {
 	if(draw_control.range_all == true)
@@ -967,15 +849,15 @@ void updateViewingRange(f32 frametime_in, Client *client)
 	//counter = 0.1;
 	counter = 0.2;
 
-	dstream<<__FUNCTION_NAME
+	/*dstream<<__FUNCTION_NAME
 			<<": Collected "<<added_frames<<" frames, total of "
-			<<added_frametime<<"s."<<std::endl;
+			<<added_frametime<<"s."<<std::endl;*/
 	
-	dstream<<"draw_control.blocks_drawn="
+	/*dstream<<"draw_control.blocks_drawn="
 			<<draw_control.blocks_drawn
 			<<", draw_control.blocks_would_have_drawn="
 			<<draw_control.blocks_would_have_drawn
-			<<std::endl;
+			<<std::endl;*/
 	
 	float range_min = g_settings.getS16("viewing_range_nodes_min");
 	float range_max = g_settings.getS16("viewing_range_nodes_max");
@@ -1001,12 +883,12 @@ void updateViewingRange(f32 frametime_in, Client *client)
 	float wanted_frametime = 1.0 / wanted_fps;
 	
 	f32 wanted_frametime_change = wanted_frametime - frametime;
-	dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
+	//dstream<<"wanted_frametime_change="<<wanted_frametime_change<<std::endl;
 	
 	// If needed frametime change is very small, just return
 	if(fabs(wanted_frametime_change) < wanted_frametime*0.2)
 	{
-		dstream<<"ignoring small wanted_frametime_change"<<std::endl;
+		//dstream<<"ignoring small wanted_frametime_change"<<std::endl;
 		return;
 	}
 
@@ -1038,11 +920,11 @@ void updateViewingRange(f32 frametime_in, Client *client)
 	if(time_per_range < min_time_per_range)
 	{
 		time_per_range = min_time_per_range;
-		dstream<<"time_per_range="<<time_per_range<<" (min)"<<std::endl;
+		//dstream<<"time_per_range="<<time_per_range<<" (min)"<<std::endl;
 	}
 	else
 	{
-		dstream<<"time_per_range="<<time_per_range<<std::endl;
+		//dstream<<"time_per_range="<<time_per_range<<std::endl;
 	}
 
 	f32 wanted_range_change = wanted_frametime_change / time_per_range;
@@ -1050,28 +932,28 @@ void updateViewingRange(f32 frametime_in, Client *client)
 	//wanted_range_change *= 0.9;
 	//wanted_range_change *= 0.75;
 	wanted_range_change *= 0.5;
-	dstream<<"wanted_range_change="<<wanted_range_change<<std::endl;
+	//dstream<<"wanted_range_change="<<wanted_range_change<<std::endl;
 
 	// If needed range change is very small, just return
 	if(fabs(wanted_range_change) < 0.001)
 	{
-		dstream<<"ignoring small wanted_range_change"<<std::endl;
+		//dstream<<"ignoring small wanted_range_change"<<std::endl;
 		return;
 	}
 
 	new_range += wanted_range_change;
-	dstream<<"new_range="<<new_range/*<<std::endl*/;
+	//dstream<<"new_range="<<new_range/*<<std::endl*/;
 	
-	float new_range_unclamped = new_range;
+	//float new_range_unclamped = new_range;
 	if(new_range < range_min)
 		new_range = range_min;
 	if(new_range > range_max)
 		new_range = range_max;
 	
-	if(new_range != new_range_unclamped)
+	/*if(new_range != new_range_unclamped)
 		dstream<<", clamped to "<<new_range<<std::endl;
 	else
-		dstream<<std::endl;
+		dstream<<std::endl;*/
 
 	draw_control.wanted_range = new_range;
 
diff --git a/src/map.cpp b/src/map.cpp
index b092e1fa538780dcfc656cd2d47246a4b6f52c83..1bd4f299f05f6eb75884641e30170151a930a825 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -26,45 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "voxel.h"
 #include "porting.h"
 
-#if 0
-MapBlockPointerCache::MapBlockPointerCache(Map *map)
-{
-	m_map = map;
-	m_map->m_blockcachelock.cacheCreated();
-
-	m_from_cache_count = 0;
-	m_from_map_count = 0;
-}
-
-MapBlockPointerCache::~MapBlockPointerCache()
-{
-	m_map->m_blockcachelock.cacheRemoved();
-
-	/*dstream<<"MapBlockPointerCache:"
-			<<" from_cache_count="<<m_from_cache_count
-			<<" from_map_count="<<m_from_map_count
-			<<std::endl;*/
-}
-
-MapBlock * MapBlockPointerCache::getBlockNoCreate(v3s16 p)
-{
-	core::map<v3s16, MapBlock*>::Node *n = NULL;
-	n = m_blocks.find(p);
-	if(n != NULL)
-	{
-		m_from_cache_count++;
-		return n->getValue();
-	}
-	
-	m_from_map_count++;
-	
-	// Throws InvalidPositionException if not found
-	MapBlock *b = m_map->getBlockNoCreate(p);
-	m_blocks[p] = b;
-	return b;
-}
-#endif
-
 /*
 	Map
 */
@@ -1573,12 +1534,12 @@ MapSector * ServerMap::emergeSector(v2s16 p2d)
 			tree_max = a / (t/0.03);
 		else
 			tree_max = a;
-		u32 count = (rand()%(tree_max+1));
+		u32 count = (myrand()%(tree_max+1));
 		//u32 count = tree_max;
 		for(u32 i=0; i<count; i++)
 		{
-			s16 x = (rand()%(MAP_BLOCKSIZE-2))+1;
-			s16 z = (rand()%(MAP_BLOCKSIZE-2))+1;
+			s16 x = (myrand()%(MAP_BLOCKSIZE-2))+1;
+			s16 z = (myrand()%(MAP_BLOCKSIZE-2))+1;
 			s16 y = sector->getGroundHeight(v2s16(x,z))+1;
 			if(y < WATER_LEVEL)
 				continue;
@@ -1597,11 +1558,11 @@ MapSector * ServerMap::emergeSector(v2s16 p2d)
 			bush_max = (pitness*a*4);
 		if(bush_max > a)
 			bush_max = a;
-		u32 count = (rand()%(bush_max+1));
+		u32 count = (myrand()%(bush_max+1));
 		for(u32 i=0; i<count; i++)
 		{
-			s16 x = rand()%(MAP_BLOCKSIZE-0)+0;
-			s16 z = rand()%(MAP_BLOCKSIZE-0)+0;
+			s16 x = myrand()%(MAP_BLOCKSIZE-0)+0;
+			s16 z = myrand()%(MAP_BLOCKSIZE-0)+0;
 			s16 y = sector->getGroundHeight(v2s16(x,z))+1;
 			if(y < WATER_LEVEL)
 				continue;
@@ -1614,11 +1575,11 @@ MapSector * ServerMap::emergeSector(v2s16 p2d)
 	*/
 	if(m_params.ravines_amount != 0)
 	{
-		if(rand()%(s32)(20.0 / m_params.ravines_amount) == 0)
+		if(myrand()%(s32)(20.0 / m_params.ravines_amount) == 0)
 		{
 			s16 s = 6;
-			s16 x = rand()%(MAP_BLOCKSIZE-s*2-1)+s;
-			s16 z = rand()%(MAP_BLOCKSIZE-s*2-1)+s;
+			s16 x = myrand()%(MAP_BLOCKSIZE-s*2-1)+s;
+			s16 z = myrand()%(MAP_BLOCKSIZE-s*2-1)+s;
 			/*s16 x = 8;
 			s16 z = 8;*/
 			s16 y = sector->getGroundHeight(v2s16(x,z))+1;
@@ -1737,11 +1698,11 @@ MapBlock * ServerMap::emergeBlock(
 	const s32 ued_max = 5;
 	const s32 ued_min = 3;
 	bool underground_emptiness[ued_max*ued_max*ued_max];
-	s32 ued = (rand()%(ued_max-ued_min+1))+1;
+	s32 ued = (myrand()%(ued_max-ued_min+1))+1;
 	//s32 ued = ued_max;
 	for(s32 i=0; i<ued*ued*ued; i++)
 	{
-		underground_emptiness[i] = ((rand() % 5) == 0);
+		underground_emptiness[i] = ((myrand() % 5) == 0);
 	}
 
 	/*
@@ -1830,9 +1791,9 @@ MapBlock * ServerMap::emergeBlock(
 		*/
 
 		v3f orp(
-			(float)(rand()%ued)+0.5,
-			(float)(rand()%ued)+0.5,
-			(float)(rand()%ued)+0.5
+			(float)(myrand()%ued)+0.5,
+			(float)(myrand()%ued)+0.5,
+			(float)(myrand()%ued)+0.5
 		);
 
 		// Check z-
@@ -1911,13 +1872,13 @@ MapBlock * ServerMap::emergeBlock(
 		for(u16 i=0; i<3; i++)
 		{
 			v3f rp(
-				(float)(rand()%ued)+0.5,
-				(float)(rand()%ued)+0.5,
-				(float)(rand()%ued)+0.5
+				(float)(myrand()%ued)+0.5,
+				(float)(myrand()%ued)+0.5,
+				(float)(myrand()%ued)+0.5
 			);
 			s16 min_d = 0;
 			s16 max_d = 4;
-			s16 rs = (rand()%(max_d-min_d+1))+min_d;
+			s16 rs = (myrand()%(max_d-min_d+1))+min_d;
 			
 			v3f vec = rp - orp;
 
@@ -2098,12 +2059,12 @@ MapBlock * ServerMap::emergeBlock(
 		*/
 		for(s16 i=0; i<underground_level*1; i++)
 		{
-			if(rand()%2 == 0)
+			if(myrand()%2 == 0)
 			{
 				v3s16 cp(
-					(rand()%(MAP_BLOCKSIZE-2))+1,
-					(rand()%(MAP_BLOCKSIZE-2))+1,
-					(rand()%(MAP_BLOCKSIZE-2))+1
+					(myrand()%(MAP_BLOCKSIZE-2))+1,
+					(myrand()%(MAP_BLOCKSIZE-2))+1,
+					(myrand()%(MAP_BLOCKSIZE-2))+1
 				);
 
 				MapNode n;
@@ -2111,14 +2072,14 @@ MapBlock * ServerMap::emergeBlock(
 				
 				//if(is_ground_content(block->getNode(cp).d))
 				if(block->getNode(cp).d == CONTENT_STONE)
-					if(rand()%8 == 0)
+					if(myrand()%8 == 0)
 						block->setNode(cp, n);
 
 				for(u16 i=0; i<26; i++)
 				{
 					//if(is_ground_content(block->getNode(cp+g_26dirs[i]).d))
 					if(block->getNode(cp+g_26dirs[i]).d == CONTENT_STONE)
-						if(rand()%8 == 0)
+						if(myrand()%8 == 0)
 							block->setNode(cp+g_26dirs[i], n);
 				}
 			}
@@ -2131,16 +2092,16 @@ MapBlock * ServerMap::emergeBlock(
 		u16 coal_rareness = 60 / coal_amount;
 		if(coal_rareness == 0)
 			coal_rareness = 1;
-		if(rand()%coal_rareness == 0)
+		if(myrand()%coal_rareness == 0)
 		{
-			u16 a = rand() % 16;
+			u16 a = myrand() % 16;
 			u16 amount = coal_amount * a*a*a / 1000;
 			for(s16 i=0; i<amount; i++)
 			{
 				v3s16 cp(
-					(rand()%(MAP_BLOCKSIZE-2))+1,
-					(rand()%(MAP_BLOCKSIZE-2))+1,
-					(rand()%(MAP_BLOCKSIZE-2))+1
+					(myrand()%(MAP_BLOCKSIZE-2))+1,
+					(myrand()%(MAP_BLOCKSIZE-2))+1,
+					(myrand()%(MAP_BLOCKSIZE-2))+1
 				);
 
 				MapNode n;
@@ -2150,14 +2111,14 @@ MapBlock * ServerMap::emergeBlock(
 				
 				//if(is_ground_content(block->getNode(cp).d))
 				if(block->getNode(cp).d == CONTENT_STONE)
-					if(rand()%8 == 0)
+					if(myrand()%8 == 0)
 						block->setNode(cp, n);
 
 				for(u16 i=0; i<26; i++)
 				{
 					//if(is_ground_content(block->getNode(cp+g_26dirs[i]).d))
 					if(block->getNode(cp+g_26dirs[i]).d == CONTENT_STONE)
-						if(rand()%8 == 0)
+						if(myrand()%8 == 0)
 							block->setNode(cp+g_26dirs[i], n);
 				}
 			}
@@ -2172,9 +2133,9 @@ MapBlock * ServerMap::emergeBlock(
 		//for(u16 i=0; i<2; i++)
 		{
 			v3s16 cp(
-				(rand()%(MAP_BLOCKSIZE-2))+1,
-				(rand()%(MAP_BLOCKSIZE-2))+1,
-				(rand()%(MAP_BLOCKSIZE-2))+1
+				(myrand()%(MAP_BLOCKSIZE-2))+1,
+				(myrand()%(MAP_BLOCKSIZE-2))+1,
+				(myrand()%(MAP_BLOCKSIZE-2))+1
 			);
 
 			// Check that the place is empty
@@ -2295,18 +2256,18 @@ MapBlock * ServerMap::emergeBlock(
 				n.d = CONTENT_STONE;
 				MapNode n2;
 				n2.d = CONTENT_AIR;
-				s16 depth = maxdepth + (rand()%10);
+				s16 depth = maxdepth + (myrand()%10);
 				s16 z = 0;
 				s16 minz = -6 - (-2);
 				s16 maxz = 6 -1;
 				for(s16 x=-6; x<=6; x++)
 				{
-					z += -1 + (rand()%3);
+					z += -1 + (myrand()%3);
 					if(z < minz)
 						z = minz;
 					if(z > maxz)
 						z = maxz;
-					for(s16 y=depth+(rand()%2); y<=6; y++)
+					for(s16 y=depth+(myrand()%2); y<=6; y++)
 					{
 						/*std::cout<<"("<<p2.X<<","<<p2.Y<<","<<p2.Z<<")"
 								<<std::endl;*/
diff --git a/src/mapblock.cpp b/src/mapblock.cpp
index 186256589fded7eaac1514379d2701b151adf5b7..60efa2fcfa1f015728fb3966d2df9d413b0d622b 100644
--- a/src/mapblock.cpp
+++ b/src/mapblock.cpp
@@ -1021,11 +1021,11 @@ void MapBlock::stepObjects(float dtime, bool server, u32 daynight_ratio)
 		m_spawn_timer -= dtime;
 		if(m_spawn_timer <= 0.0)
 		{
-			m_spawn_timer += rand() % 300;
+			m_spawn_timer += myrand() % 300;
 			
 			v2s16 p2d(
-				(rand()%(MAP_BLOCKSIZE-1))+0,
-				(rand()%(MAP_BLOCKSIZE-1))+0
+				(myrand()%(MAP_BLOCKSIZE-1))+0,
+				(myrand()%(MAP_BLOCKSIZE-1))+0
 			);
 
 			s16 y = getGroundLevel(p2d);
diff --git a/src/mapblockobject.h b/src/mapblockobject.h
index 08f535514f7ca94c7fde155533d056dd7120b265..aee41f3ba1620cb7e48eb7f08015932ab8f59958 100644
--- a/src/mapblockobject.h
+++ b/src/mapblockobject.h
@@ -630,8 +630,8 @@ class RatObject : public MovingObject
 			m_counter2 -= dtime;
 			if(m_counter2 < 0.0)
 			{
-				m_counter2 += (float)(rand()%100)/100*3.0;
-				m_yaw += ((float)(rand()%200)-100)/100*180;
+				m_counter2 += (float)(myrand()%100)/100*3.0;
+				m_yaw += ((float)(myrand()%200)-100)/100*180;
 				m_yaw = wrapDegrees(m_yaw);
 			}
 		}
diff --git a/src/socket.cpp b/src/socket.cpp
index f4b8f442943aaf78834569547ab376763a65aef0..499ee504e0d7e33c3c793577a5f040b2971a8486 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <iostream>
 #include <stdlib.h>
 #include <errno.h>
+#include "utility.h"
 
 // Debug printing options
 #define DP 0
@@ -194,8 +195,8 @@ void UDPSocket::Send(const Address & destination, const void * data, int size)
 {
 	bool dumping_packet = false;
 	if(INTERNET_SIMULATOR)
-		dumping_packet = (rand()%10==0); //easy
-		//dumping_packet = (rand()%4==0); // hard
+		dumping_packet = (myrand()%10==0); //easy
+		//dumping_packet = (myrand()%4==0); // hard
 
 	if(DP){
 		/*dstream<<DPS<<"UDPSocket("<<(int)m_handle
diff --git a/src/test.cpp b/src/test.cpp
index 142918df622edd414b42a7788cd010b32fb3af5a..38c663236a2e5267cf7d9ea94c3e68e1f9bdbc65 100644
--- a/src/test.cpp
+++ b/src/test.cpp
@@ -723,9 +723,9 @@ struct TestHeightmap
 	void Random()
 	{
 		dstream<<"Running random code (get a human to check this)"<<std::endl;
-		dstream<<"rand() values: ";
+		dstream<<"myrand() values: ";
 		for(u16 i=0; i<5; i++)
-			dstream<<(u16)rand()<<" ";
+			dstream<<(u16)myrand()<<" ";
 		dstream<<std::endl;
 
 		const s16 BS1 = 8;
diff --git a/src/voxel.cpp b/src/voxel.cpp
index 55cd73893c3a6cae96f2ab27bf8785902dd3ed1b..2fd4e7b457ab4bef1f0b64f8cfd38e4ef48dba3f 100644
--- a/src/voxel.cpp
+++ b/src/voxel.cpp
@@ -893,7 +893,7 @@ void VoxelManipulator::flowWater(
 	*/
 	s32 k = 0;
 	if(active_nodes.size() != 0)
-		k = (s32)rand() % (s32)active_nodes.size();
+		k = (s32)myrand() % (s32)active_nodes.size();
 
 	// Flow water to active nodes
 	for(;;)