stack level too deep error; simplifying code