Modified to compile with g++ and fixed seg faulting issues on Macs