organized code and added a few comments