severe code refinement

I made things a little bit simpler and it is much shorter now.