refactored to use templates but still failing