Refactored code, added additional test for invalid power expression