Fixed clobbering issue.
Fix compiler errors and improve error checking.