Fix multiple bugs and improve interface