Made the functions a litle more safe. There is still a bug somewhere. Just gotta find it