Rewritten generateAB function and added tests