attempting to modularize this code