moved code from duplicate function