Fixed some bugs, added some functionality.

Haven't debugged all the code though and some functions left to add.