Made code compaitable with C++ compilers

Also moved some functions.