fixed potential memory leak, other bugs etc...