Better handling of errors and style fixes