exception handling code and bug fixes