Added some type checking and asserts