refractured the code and made it a bit more clean