Added exception handling where needed.