various little fixes (one was critical though)
better use of str_replace
remove dead assignments
fix issue #77 bad logic