Refactored type-checking. Re-ordered some declarations to avoid a compiler warning and to fix the underlying bug.