Fixed error check for OSX and implemented some OpenGL cleanup