Implemented statement type checking

And a lot of refactoring