Code cleaned up and commented