improved logic for tempate system