huge refactor to boost the performance