fix a lot of bugs; better API