Fixed #14 and a few other bugs