using "goto" for proper cleanup on error