simplified logic and broke today's work down into separate functions which may be called from different places