Fixed inefficient code (same count query twice)