A bit more cleanup.
Added some addition type-safety checks.