Update code and fix bug