Updated the main functions

Works slightly better with  strange values....