Added some performance related changes proposed by Phan Shi Wen.