Update Algorithm

Fixed some bugs