more refactor and cleanup, edited some of mike's changes