cleaned up and commented code