Added type checking for g-functions. Refactored code.