Bugs fixed

Added bug fixes and more detailed error reporting.