Added new functions and fixed a lot of bugs