refactored peed a lot and removed some bugs relating to newer php versions