Fixed bugs, added some functions