Added few exceptions treatment and code cleanup