added some time complexity analysis, reformatted some comments