fixed logic bugs. The rules where not applied properly! :/
updated link with live code