bug fixes and exception handling