Initial type casting, bug fixes