More more on type-safing.

Also splitting functionality into multiple files