Add ElementImproperStateException

Add ElementImproperStateException and make some minor improvements.