Fixing some bugs and adding some functions