Bug fixes and instanceof checks