created main logic for non-smooth problems. TODO: fix sudden crashing