cleaned up chunk generation code