cleaned up code a bit in preparation for a recursive solution