Updated code, fixed some bugs