Fix bug with computing count and appending to database