use public API for error handling; improved while condition