Added includes. Added comments. Various bug fixes.