breakup code into smaller pieces, cleanup