Cleaned up q_shared.h q_shared.cpp and q_math.cpp

Also replaced some VM replacement methods with the library ones.