Fixed some exceptions.
Next time i'll need to make this code more readable (by just cutting one
big method to many smaller ones).