improved; still to refactor