reworking andrew's code