Fixed some bugs and cleaner API