almost completed permuation code