Misc bugfixes and API update

Added API functionality.
Fixed misc bugs.