rewrote the analyzing part in a more funciotal style