trying to modularize some of this code