Some bug fixes + better error reporting