More work towards proper type handling. There are still some edge cases to sort out.