Fixed #15, #5 and a few other bugs