final changes, added internal documentation, cleaned up code