Fixes some bugs and add functions