Some optimazions and a better exception-handling