added pats code and fixed bug