Added more comments and fixed a few bugs