tidy up function and comment on possible bug