Fixed some more bugs, improved error handling