major rewrite; corrected some errors