added some comments, cleaned up code