cleaned up code and annotated it more extensively. No functional changes