Added licence and fixed some bugs