Cleaned up the code. Works for inputs, which are bigger than 3 and where both concave and convex parts are present. Iterator renewal logic works perfectly, but unintuitively, and needs to be rewritten