Bugfixes and moved from mysql to mysqli