Added contracts and some bugfixes