added more comments and renamed functions