Fixed some errors, introduced exception handling, and put some love into the code ;) 

Found that there are a number of verifications and exception handling missing. Took the liberty to care about;)