Fixed memory leak by making tweaks on the I/O side (Still needs some
testing, but looks quite good.)