updated the code with small fixes and comments. Found a problem in the code that needs to be fixed. Still needs to be tested.