Improved error checking, comments; variable names more meaningful and consistent (for Gastil)