Fix issue #22, fix minor bugs