code clean up, add comments