Added licence headers and some bugfixes