Fixed non-static counting and SQL connection