fixed some small bugs
Improved error handling