Added exceptions
Reorganized code