debugged mathematical function