refactored; runs faster with PyPy, but still slow; added test case; submitted result (34) not accepted, still running