some code cleanup
added more comments