improved analyze function