Fix some bugs; prepare for npm