fixed all errors/unsafe behaviour reported by valgrind