Fixed most of the bugs. Added new functions