Refactoring code and fixing some GL bugs