finished simplifying algorithm - seems to work fine